package com.hellowx.qydcdev.controller;

import com.github.pagehelper.PageInfo;
import com.hellowx.qydcdev.annotaion.RequiredToken;
import com.hellowx.qydcdev.entity.OrderPay;
import com.hellowx.qydcdev.entity.OrderVO;
import com.hellowx.qydcdev.exception.CustomException;
import com.hellowx.qydcdev.exception.CustomExceptionType;
import com.hellowx.qydcdev.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created on 2022/1/10.
 *
 * @author Hello WX
 */
@Api(tags = "订单模块")
@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private OrderService orderService;

    @ApiOperation("模拟支付功能")
    @PostMapping("/pay")
    public ResponseEntity<String> simulatePayment(long orderId){
        boolean flag = orderService.toPay(orderId);
        if (flag){
            return ResponseEntity.ok("支付成功!!");
        }
        throw new CustomException(CustomExceptionType.USER_INPUT_EXCEPTION,"支付失败");
    }

    @ApiOperation("获取该顾客的订单列表,带分页")
    @GetMapping("/customerId")
    public ResponseEntity<PageInfo<OrderVO>> getOrderListByCustomerId(@RequestParam("customerId") Long customerId,
                                                                  @RequestParam("pageNum") int pageNum,
                                                                      @RequestParam("pageSize") int pageSize){
        PageInfo<OrderVO> orderVOListPage = orderService.listOrderByCustomerId(customerId,pageNum,pageSize);
        return ResponseEntity.ok(orderVOListPage);
    }


    @ApiOperation("获取当前用户(店铺)的订单信息")
    @GetMapping("/currentUser")
    @RequiredToken
    public ResponseEntity<PageInfo<OrderPay>> getOrderListByCurrentUser(@RequestParam("pageNum") int pageNum,
                                                                       @RequestParam("pageSize") int pageSize,
                                                                       @RequestParam(value = "orderId",required = false) Long orderId,
                                                                       @RequestParam(value = "payNo",required = false) Long payNo,
                                                                       HttpServletRequest request){
        PageInfo<OrderPay> orderVOListPage = orderService.listOrderBySearch(orderId,payNo,pageNum,pageSize,request);
        return ResponseEntity.ok(orderVOListPage);
    }

    @ApiOperation("通过订单id获取订单信息")
    @GetMapping("/byOrderId")
    public ResponseEntity<OrderVO> getOrderVOByOrderId(@RequestParam("orderId") long orderId){
        OrderVO orderVO = orderService.getOrderByOrderId(orderId);
        return ResponseEntity.ok(orderVO);
    }

    @ApiOperation("模拟退订")
    @PostMapping("/unsubscribe")
    public ResponseEntity<String> updateStatus2(Long orderId){
        String msg = orderService.unsubscribe(orderId,2);
        return ResponseEntity.ok(msg);
    }

    @ApiOperation("模拟退订成功")
    @RequiredToken
    @PostMapping("/unsubscribeOk")
    public ResponseEntity<String> updateStatus3(Long orderId){
        String msg = orderService.unsubscribe(orderId,3);
        return ResponseEntity.ok(msg);
    }
}
